package com.gp.study.mybatis.bind;

import java.util.HashMap;
import java.util.Map;

import com.gp.study.mybatis.session.DefaultSqlSession;

public class MapperProxyRegistry {
	
	// 接口和工厂类映射关系
    private final Map<Class<?>, MapperProxyFactory> knownMappers = new HashMap<>();
    
    /**
     * Configuration 里面  扫描解析Mapper ，注册进来，
     * @param clazz  此处传入pojo类型，是为了最终处理结果集的时候将结果转换为POJO类型
     */
    public <T> void addMapper(Class<T> clazz){
    	knownMappers.put(clazz,new MapperProxyFactory(clazz));
    }
    
    public <T> T getMapper(Class<T> clazz, DefaultSqlSession sqlSession) {
    	MapperProxyFactory mapperProxyFactory = knownMappers.get(clazz);
    	if(null == mapperProxyFactory){
    		throw new RuntimeException("class type "+clazz+"not found");
    	}
    	return mapperProxyFactory.newInstance(sqlSession);
    }
}
